package cn.restcloud.example.controller;

import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.cache.CacheFactory;
import cn.restcloud.framework.core.util.DateTimeUtil;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

/**
 * 本地缓存使用Demo
 * @author Thinkpad
 *
 */

@BeanConfig(appId = "example",beanId = "CacheManagerRest",beanName = "本地缓存使用示例",beanType = BeanType.CONTROLLER,autowired = true,singleton = true,hotMode = true)
@RestConfig(url = "/rest/example/cache")
public class CacheManagerRest {
	private String cacheName="ACacheManagerDemo"; //要缓存的策略名称
	private String cacheKey="CacheTestData"; //要缓存的关键字
	
	@RestConfig(configName = "保存数据到本地缓存中",method = MethodType.GET,url = "/write/cache")
	public DocAndView writeCache() throws Exception{
		CacheFactory.put(cacheName, cacheKey, "要缓存的值="+DateTimeUtil.getNow(),60); //60表示缓存过期时间为60秒
		return ViewUtil.getSuccessView("缓存成功");
	}
	
	@RestConfig(configName = "读取本地缓存数据",method = MethodType.GET,url = "/read/cache")
	public DocAndView readCache() throws Exception{
		String cacheValue=(String) CacheFactory.get(cacheName, cacheKey);
		if(cacheValue==null){
			return ViewUtil.getErrorView("缓存不存在");
		}else{
			return ViewUtil.getSuccessView(cacheValue);
		}
	}
	
}
